home *** CD-ROM | disk | FTP | other *** search
- unit DrBobCGI;
- {$I-}
- interface
-
- type
- TRequestMethod = (Unknown,Get,Post);
- var
- RequestMethod: TRequestMethod = Unknown;
-
- var
- ContentLength: Integer = 0;
- Data: AnsiString = '';
-
- function Value(const Field: ShortString): ShortString;
-
- function ValueAsInteger(const Field: ShortString): Integer;
-
- implementation
- uses
- SysUtils, Windows;
-
- function Value(const Field: ShortString): ShortString;
- var
- i: Integer;
- len: Byte absolute Result;
- begin
- Len := 0;
- i := Pos('&'+Field+'=',Data);
- if i = 0 then
- begin
- i := Pos(Field+'=',Data);
- if i > 1 then i := 0
- end
- else Inc(i); { skip '&' }
- if i > 0 then
- begin
- Inc(i,Length(Field)+1);
- while Data[i] <> '&' do
- begin
- if not (Data[i] in [#10,#13]) then { ignore CR/LF }
- begin
- Inc(Len);
- Result[Len] := Data[i]
- end
- else { CR/LF -> #32 }
- begin
- if (Len = 0) or (Result[Len] <> #32) then
- begin
- Inc(Len);
- Result[Len] := #32
- end
- end;
- Inc(i)
- end
- end;
- while (Len > 0) and (Result[len] = #32) do Dec(len)
- end {Value};
-
- function ValueAsInteger(const Field: ShortString): Integer;
- begin
- Result := StrToInt(Value(Field))
- end {ValueAsInteger};
-
- var
- P: PChar;
- i: Integer;
- Str: ShortString;
-
- initialization
- P := GetEnvironmentStrings;
- while P^ <> #0 do
- begin
- Str := StrPas(P);
- if Pos('REQUEST_METHOD=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- if Str = 'POST' then RequestMethod := Post
- else
- if Str = 'GET' then RequestMethod := Get
- end;
- if Pos('CONTENT_LENGTH=',Str) = 1 then
- begin
- Delete(Str,1,Pos('=',Str));
- ContentLength := StrToInt(Str)
- end;
- if Pos('QUERY_STRING=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- SetLength(Data,Length(Str)+1);
- Data := Str
- end;
- Inc(P, StrLen(P)+1)
- end;
- if RequestMethod = Post then
- begin
- SetLength(Data,ContentLength+2);
- for i:=1 to ContentLength do read(Data[i]);
- Data[ContentLength+1] := '&';
- { if IOResult <> 0 then { skip }
- end;
- i := 0;
- while i < Length(Data) do
- begin
- Inc(i);
- if Data[i] = '+' then Data[i] := ' ';
- if Data[i] = '%' then { special code }
- begin
- Str := '$00';
- Str[2] := Data[i+1];
- Str[3] := Data[i+2];
- Delete(Data,i+1,2);
- Data[i] := Chr(StrToInt(Str))
- end
- end;
- if i > 0 then Data[i+1] := '&'
- else Data := '&'
- finalization
- Data := ''
- end.
-